<?php

require_once "sql_helper.class.php";

class User {

    public static function login($u_username, $u_password) {
        if ($u_username != NULL && $u_password != NULL) {
            /// Lets transfer password into md5
            $u_password = md5($u_password);

            /// Connecting to MySQL and Selecting database
            $con = mysql_connect("ltuprj.qv.vic.edu.au", "Cerberus", "mEtaekdT") or die(mysql_error());

            if (!$con) {
                die('Could not connect: ' . mysql_error());
            }
            $statement = "SELECT userID, loginName, userName, emailAddress, userRole FROM `C-dev`.USER Where loginName = '$u_username' AND userPass = '$u_password';";

            $result = SQLHelper::executeQuerry($statement);

            $row = mysql_fetch_object($result);

            return $row;
        }

    }

    public static function getUser($userID)
    {
        $statement = "SELECT * FROM `C-dev`.USER WHERE userID = $userID;";
        $result = SQLHelper::executeQuerry($statement);

        $row = mysql_fetch_object($result);

        return $row;
    }

    public static function getUsers()
    {
        $statement = "SELECT * FROM `C-dev`.USER";
        $result = SQLHelper::executeQuerry($statement);

        return $result;
    }

    public static function getFBUser($fbID)
    {
        $statement = "SELECT * FROM `C-dev`.USER WHERE userFacebook = '$fbID';";

        $result = SQLHelper::executeQuerry($statement);

        $row = mysql_fetch_object($result);

        return $row;
    }

    public static function addUser($userName, $userPassword, $userEmail, $userType)
    {
        $userPassword = md5($userPassword);

        $statement = "INSERT INTO `C-dev`.USER(userName, loginName, userPass, emailAddress, userRole, userAvatar) VALUES('$userName', '$userName', '$userPassword', '$userEmail', '$userType', null)";

        SQLHelper::executeNonQuerry($statement);
    }

    public static function addUserFB($userName, $userPassword, $userEmail, $userType, $fbID)
    {
        $userPassword = md5($userPassword);

        $statement = "INSERT INTO `C-dev`.USER(userName, loginName, userPass, emailAddress, userRole, userAvatar, userFacebook) VALUES('$userName', '$userName', '$userPassword', '$userEmail', '$userType', null, '$fbID')";

        SQLHelper::executeNonQuerry($statement);
    }

    public static function updateUserFacebook($userID, $fbID)
    {
        $statement = "UPDATE `C-dev`.USER SET userFacebook = '$fbID' WHERE  userID = $userID";
        echo $statement;
        SQLHelper::executeNonQuerry($statement);
    }
}
?>

